package com.horizon.manage.student.converter;

import com.horizon.common.constrants.HorizonConstants;
import com.horizon.common.util.HorizonUtil;
import com.horizon.manage.student.entity.Student;
import com.horizon.manage.student.vo.StudentVO;
import com.horizon.modules.system.entity.User;
import com.horizon.modules.system.service.IUserService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.math.NumberUtils;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springframework.stereotype.Component;

/**
 * 学生实体视图转换器.
 *
 * @author zhaoshb
 * @since 2022/12/3 22:46
 */
@Component
@AllArgsConstructor
public class StudentEVConverter extends BaseEntityWrapper<Student, StudentVO> {

    private IUserService userService;

    @Override
    public StudentVO entityVO(Student entity) {
        StudentVO studentVO = new StudentVO();
        fillByStudent(studentVO, entity);
        fillByUser(studentVO, userService.getById(entity.getUserId()));
        return studentVO;
    }

    private void fillByStudent(StudentVO vo, Student student) {
        vo.setNumber(student.getNumber());
        vo.setStatusNumber(student.getStatusNumber());
        vo.setIdCardNumber(student.getIdCardNumber());
        vo.setId(student.getId());
        vo.setPhotoUrl(student.getPhotoUrl());
    }

    private void fillByUser(StudentVO vo, User user) {
        vo.setName(user.getName());
        vo.setAccount(user.getAccount());
        vo.setClassId(user.getDeptId());
        vo.setBirthday(user.getBirthday());
        vo.setSex(user.getSex());
        vo.setSexShowName(getSexShowName(user.getSex()));
        vo.setStatus(user.getStatus());
    }

    private String getSexShowName(Integer sex) {
        return NumberUtils.INTEGER_ZERO.equals(sex) ? HorizonUtil.getLocaleMessage(HorizonConstants.LC_USER_SEX_MALE) : HorizonUtil.getLocaleMessage(HorizonConstants.LC_USER_SEX_FEMALE);
    }

}
